package org.derusse.cycle.model;

import java.util.ArrayList;
import java.util.List;

import org.derusse.cycle.CycleMeterApplication;
import org.derusse.cycle.event.LocationLiveListener;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class Live implements LocationListener {
	public static final int STATUS_DISABLED = 0;
	public static final int STATUS_WAITING = 1;
	public static final int STATUS_READY = 2;

	private Location lastLocation;
	private LocationManager locationManager;

	private List<LocationLiveListener> liveListeners = new ArrayList<LocationLiveListener>();

	private int status = 0;

	public Live(CycleMeterApplication application) {
		this.locationManager = application.getLocationManager();
	}

	public void addListener(LocationLiveListener listener) {
		liveListeners.add(listener);
	}

	public void removeListener(LocationLiveListener listener) {
		liveListeners.remove(listener);
	}

	public int getStatus() {
		return this.status;
	}

	private void setStatus(int value) {
		if (status == value)
			return;
		int oldStatus = this.status;
		this.status = value;
		int countListener = this.liveListeners.size();
		for (int i = 0; i < countListener; i++) {
			this.liveListeners.get(i).onLiveStatusChanged(oldStatus,
					value);
		}
	}

	public void Start() {
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				200, 0, this);
	}

	public void Stop() {
		locationManager.removeUpdates(this);
	}

	public void onLocationChanged(Location location) {
		if(location.hasSpeed() && location.getSpeed()>100) return;
		
		this.lastLocation = location;
		int countListener = this.liveListeners.size();
		for (int i = 0; i < countListener; i++) {
			this.liveListeners.get(i).onLiveChanged();
		}
	}

	public void onProviderDisabled(String provider) {
		this.setStatus(STATUS_DISABLED);
	}

	public void onProviderEnabled(String provider) {
		this.setStatus(STATUS_WAITING);
	}

	public void onStatusChanged(String provider, int status, Bundle extraBundle) {
		this.setStatus(status);
	}

	public void onGpsChanged(int oldStatus, int newStatus) {
	}

	
	public String getLatitudeFormat() {
		if (this.lastLocation == null || this.status != STATUS_READY)
			return "";
		return FormatCoordinate(this.lastLocation.getLatitude());
	}

	public String getLongitudeFormat() {
		if (this.lastLocation == null || this.status != STATUS_READY)
			return "";
		return FormatCoordinate(this.lastLocation.getLongitude());
	}
	private static String FormatCoordinate(double value) {
		return String.format("%.06f", value);
	}

	public String getSpeedFormat() {
		if (this.lastLocation == null ||this.status != STATUS_READY || !this.lastLocation.hasSpeed())
			return "-";
		return String.format("%.01f", this.lastLocation.getSpeed() * 3.6);
	}

	public String getAltitudeFormat() {
		if (this.lastLocation == null ||this.status != STATUS_READY || !this.lastLocation.hasAltitude())
			return "-";
		return String.format("%.01f", this.lastLocation.getAltitude());
	}

	public String getAccuracyFormat() {
		if (this.lastLocation == null ||this.status != STATUS_READY || !this.lastLocation.hasAccuracy())
			return "-";
		return String.format("%.00f", this.lastLocation.getAccuracy());
	}

}